package cn.hdu.liang.common.web;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 异步返回各种格式
 * json,xml,text
 */
public class ResponseUtils {
    private static void render(HttpServletResponse resp, String contentType, String text) {
        resp.setContentType(contentType);
        try {
            resp.getWriter().write(text);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void renderJson(HttpServletResponse resp, String text) {
        render(resp, "application/json;charset=UTF-8", text);
    }

    public static void renderXml(HttpServletResponse resp, String text) {
        render(resp, "text/xml;charset=UTF-8", text);
    }

    public static void renderText(HttpServletResponse resp, String text) {
        render(resp, "text/plain;charset=UTF-8", text);
    }
}
